
import { BuildData, BuildUnit, GarbageRoomData, RoomData, Season } from "@/type";
import { observable, configure, toJS } from "mobx";
import { generateUUID } from "three/src/math/MathUtils";
import mapPNG from "./map.png";
import map1 from "./地图1.png";
import map2JPG from "./map2.jpg";
import map33 from "./地图33.jpeg";
import { isEqual } from "lodash-es";
import { nanoid } from 'nanoid'
import utils from "@/utils/utils";

configure({ enforceActions: "never" });

const homeStore = window.homeStore = observable({
    // 最后数据更新时间
    latestUpdata: 0,
    async updata() {
        await new Promise(resolve => setTimeout(resolve, 30));
        this.latestUpdata++;
    },

    //地板贴图地址
    floorUrl: map1,
    setFloorUrl(floorUrl: string = "") {
        this.floorUrl = floorUrl;
    },

    //颜色数组
    colors: ["#015850", "#309E95", "#2185B6", "#3AA3D2", "#784DC1", "#A06FEC", "#9F8167", "#BCB5A5", "#B47E7E", "#C19F9D", "#5F7DAF", "#8FAAD5"],


    //添加一栋楼
    addBuild() {
        const id = nanoid(6);
        const newBuildData: BuildData = {
            id,
            name: "100#",
            buildUnits: [
                {
                    id: nanoid(6), // 楼栋单元的id
                    name: "1", // 单元名称
                    unitTypes: ['90户型\n3/2/3', '100户型\n3/2/3'], // 户型名称数据和列数相同
                    roomDatas: [
                        {
                            id: nanoid(6), // 楼栋单元的id
                            floorTopColor: this.colors[utils.rand(0, this.colors.length - 1)], // 楼顶的户型颜色
                            roomDatas: [
                                { id: nanoid(6), name: "1-101", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-201", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-301", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-401", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-501", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-601", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-701", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-801", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-901", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1001", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1101", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1201", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1301", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1401", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1501", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1601", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1701", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1801", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1901", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-2001", color: "#ffffff", floorHeight: 3 },
                            ]
                        },
                        {
                            id: nanoid(6), // 楼栋单元的id
                            floorTopColor: this.colors[utils.rand(0, this.colors.length - 1)], // 楼顶的户型颜色
                            roomDatas: [
                                { id: nanoid(6), name: "1-101", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-201", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-301", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-401", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-501", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-601", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-701", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-801", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-901", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1001", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1101", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1201", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1301", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1401", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1501", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1601", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1701", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1801", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1901", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-2001", color: "#ffffff", floorHeight: 3 },
                            ]
                        }
                    ]
                },
                {
                    id: nanoid(6), // 楼栋单元的id
                    name: "2", // 单元名称
                    unitTypes: ['110户型\n3/2/3', '120户型\n3/2/3', '130户型\n3/2/3', '130户型\n3/2/3'], // 户型名称数据和列数相同
                    roomDatas: [
                        {
                            id: nanoid(6), // 楼栋单元的id
                            floorTopColor: this.colors[utils.rand(0, this.colors.length - 1)], // 楼顶的户型颜色
                            roomDatas: [
                                { id: nanoid(6), name: "1-101", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-201", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-301", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-401", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-501", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-601", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-701", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-801", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-901", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1001", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1101", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1201", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1301", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1401", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1501", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1601", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1701", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1801", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1901", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-2001", color: "#ffffff", floorHeight: 3 },
                            ]
                        },
                        {
                            id: nanoid(6), // 楼栋单元的id
                            floorTopColor: this.colors[utils.rand(0, this.colors.length - 1)], // 楼顶的户型颜色
                            roomDatas: [
                                { id: nanoid(6), name: "1-101", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-201", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-301", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-401", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-501", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-601", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-701", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-801", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-901", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1001", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1101", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1201", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1301", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1401", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1501", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1601", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1701", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1801", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1901", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-2001", color: "#ffffff", floorHeight: 3 },
                            ]
                        },
                        {
                            id: nanoid(6), // 楼栋单元的id
                            floorTopColor: this.colors[utils.rand(0, this.colors.length - 1)], // 楼顶的户型颜色
                            roomDatas: [
                                { id: nanoid(6), name: "1-101", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-201", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-301", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-401", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-501", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-601", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-701", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-801", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-901", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1001", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1101", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1201", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1301", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1401", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1501", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1601", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1701", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1801", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1901", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-2001", color: "#ffffff", floorHeight: 3 },
                            ]
                        },
                        {
                            id: nanoid(6), // 楼栋单元的id
                            floorTopColor: this.colors[utils.rand(0, this.colors.length - 1)], // 楼顶的户型颜色
                            roomDatas: [
                                { id: nanoid(6), name: "1-101", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-201", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-301", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-401", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-501", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-601", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-701", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-801", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-901", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1001", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1101", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1201", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1301", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1401", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1501", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1601", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1701", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1801", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-1901", color: "#ffffff", floorHeight: 2 },
                                { id: nanoid(6), name: "1-2001", color: "#ffffff", floorHeight: 3 },
                            ]
                        }
                    ]
                },
            ],
            x: 9999999,
            z: 9999999,
            rotationY: 0 // 旋转角度
        }
        const buildDatas = this.getBuildDatas();
        buildDatas.push(newBuildData);
        this.setBuildDatas(buildDatas);
        this.editId = id;
        this.activeBuildId = "";
        this.activeRoomId = "";
    },

    // 编辑状态
    edit: true,
    setEdit(edit: boolean) {
        this.edit = edit;
    },

    //编辑的楼栋id
    editId: "",
    setEditId(editId: string = "") {
        this.editId = editId;
    },

    //房间的宽度
    roomWidth: 4,
    setRoomWidth(roomWidth: number) {
        this.roomWidth = roomWidth;
    },

    //房间的高度
    roomHeight: 2,
    setRoomHeigh(roomHeight: number) {
        this.roomHeight = roomHeight;
    },

    //房间的厚度
    roomThickness: 2,
    setRoomThickness(roomThickness: number) {
        this.roomThickness = roomThickness;
    },

    //当前选中的楼栋id
    activeBuildId: "",
    setActiveBuildId(activeBuildId: string = "") {
        this.activeBuildId = activeBuildId;
    },

    //当前选中的单元id
    activeBuildUnitId: "",
    setActiveBuildUnitId(activeBuildUnitId: string = "") {
        this.activeBuildUnitId = activeBuildUnitId;
    },

    //当前选中的单元id
    activeRoomId: "",
    setActiveRoomId(activeRoomId: string = "") {
        this.activeRoomId = activeRoomId;
    },

    // 计算遮挡了住了被观察的楼栋的楼栋id,并做隐藏处理
    hideBuildId: [] as Array<string>,
    setHideBuildId(hideBuildId: Array<string>) {
        if (!isEqual(this.hideBuildId, hideBuildId)) {
            this.hideBuildId = hideBuildId;
        }
    },

    // 楼栋数据
    buildDatas: [] as Array<BuildData>,
    async setBuildDatas(buildDatas: Array<any>) {
        this.buildDatas = buildDatas;
        this.updata();
    },
    getBuildDatas() {
        return toJS(this.buildDatas);
    },

    // 获取一个楼栋的数据
    getBuildDataById(id: string) {
        for (let i = 0; i < this.buildDatas.length; i++) {
            if (this.buildDatas[i].id === id) {
                return toJS(this.buildDatas[i]);
            }
        }
        return undefined;
    },

    // 设置一个楼栋的数据
    setBuildDataById(id: string, buildData: Partial<BuildData>) {
        for (let i = 0; i < this.buildDatas.length; i++) {
            if (this.buildDatas[i].id === id) {
                Object.assign(this.buildDatas[i], buildData);
            }
        }
    },

    //删除一个楼栋的数据
    delBuildDataById(id: string) {
        let buildDatas = toJS(this.buildDatas);
        buildDatas = buildDatas.filter(buildData => buildData.id !== id);
        this.setBuildDatas(buildDatas);
        this.activeBuildId = "";
        this.activeRoomId = "";
    },

    //获取一个单元数据
    getBuldUnitById(id: string) {
        const buildDatas = toJS(this.buildDatas);
        for (let i = 0; i < buildDatas.length; i++) {
            for (let ii = 0; ii < buildDatas[i].buildUnits.length; ii++) {
                if (buildDatas[i].buildUnits[ii].id === id) {
                    return buildDatas[i].buildUnits[ii];
                }
            }
        }
        return undefined;
    },

    //设置一个单元数据
    setBuldUnitById(id: string, buildUnit: Partial<BuildUnit>) {
        const buildDatas = toJS(this.buildDatas);
        for (let i = 0; i < buildDatas.length; i++) {
            for (let ii = 0; ii < buildDatas[i].buildUnits.length; ii++) {
                if (buildDatas[i].buildUnits[ii].id === id) {
                    Object.assign(buildDatas[i].buildUnits[ii], buildUnit);
                }
            }
        }
        this.setBuildDatas(buildDatas);
    },


    //获取一个房间的数据
    getRoomDataById(id: string) {
        const buildDatas = toJS(this.buildDatas);
        for (let i = 0; i < buildDatas.length; i++) {
            for (let ii = 0; ii < buildDatas[i].buildUnits.length; ii++) {
                for (let iii = 0; iii < buildDatas[i].buildUnits[ii].roomDatas.length; iii++) {
                    for (let iiii = 0; iiii < buildDatas[i].buildUnits[ii].roomDatas[iii].roomDatas.length; iiii++) {
                        if (buildDatas[i].buildUnits[ii].roomDatas[iii].roomDatas[iiii].id === id) {
                            return buildDatas[i].buildUnits[ii].roomDatas[iii].roomDatas[iiii];
                        }
                    }
                }
            }
        }
        return undefined;
    },
    setRoomDataById(id: string, roomDatas: Partial<RoomData>) {
        const buildDatas = toJS(this.buildDatas);
        for (let i = 0; i < buildDatas.length; i++) {
            for (let ii = 0; ii < buildDatas[i].buildUnits.length; ii++) {
                for (let iii = 0; iii < buildDatas[i].buildUnits[ii].roomDatas.length; iii++) {
                    for (let iiii = 0; iiii < buildDatas[i].buildUnits[ii].roomDatas[iii].roomDatas.length; iiii++) {
                        if (buildDatas[i].buildUnits[ii].roomDatas[iii].roomDatas[iiii].id === id) {
                            Object.assign(buildDatas[i].buildUnits[ii].roomDatas[iii].roomDatas[iiii], roomDatas);
                        }
                    }
                }
            }
        }
        this.setBuildDatas(buildDatas);
    },

    //是否显示门牌号码
    showRoomLable: true,
    setShowRoomLable(showRoomLable: boolean) {
        this.showRoomLable = showRoomLable;
    },

    //是否显示门牌号码背景
    showRoomLableBackground: true,
    setShowRoomLableBackground(showRoomLableBackground: boolean) {
        this.showRoomLableBackground = showRoomLableBackground;
    },

    // 垃圾房数据
    garbageRoomDatas: [] as Array<GarbageRoomData>,
    async setGarbageRoomDatas(garbageRoomDatas: Array<GarbageRoomData>) {
        this.garbageRoomDatas = garbageRoomDatas;
        this.updata();
    },
    //获取垃圾房的数据
    getGarbageRoomDatas() {
        return toJS(this.garbageRoomDatas);
    },
    //通过id获取垃圾房的数据
    getGarbageRoomDataById(id: string) {
        return toJS(this.garbageRoomDatas.find(grd => grd.id === id));
    },
    //修改垃圾房的数据
    setGarbageRoomDataById(id: string, garbageRoomData: Partial<GarbageRoomData>) {
        this.garbageRoomDatas.map(grd => {
            if (id === grd.id) {
                Object.assign(grd, garbageRoomData);
            }
        })
    },
    //删除垃圾房
    deleteGarbageRoomDataById(id: string) {
        this.garbageRoomDatas = this.garbageRoomDatas.filter(grd => (id !== grd.id));
    },

    // 选中的垃圾房
    activeGarbageRoomId: "",
    setActiveGarbageRoomId(garbageRoomId: string = "") {
        this.activeGarbageRoomId = garbageRoomId;
    },

    //编辑中的垃圾房的id
    editGarbageRoomId: "",
    setEditGarbageRoomId(editGarbageRoomId: string = "") {
        this.editGarbageRoomId = editGarbageRoomId;
    },

    //添加一个垃圾房的
    addGarbageRoom() {
        const id = nanoid(6);
        const garbageRoomData: GarbageRoomData = {
            id,
            x: 9999999,
            z: 9999999,
            rotationY: 0 // 旋转角度
        }
        const garbageRoomDatas = this.getGarbageRoomDatas();
        garbageRoomDatas.push(garbageRoomData);
        this.setGarbageRoomDatas(garbageRoomDatas);
        this.editGarbageRoomId = id;
        this.activeGarbageRoomId = "";
    },

    //季节
    season: "spring_equinox" as Season,
    setSeason(season: Season) {
        this.season = season;
    },

    //当前时间
    time: 12 * 60 * 60 * 1000,
    setTime(time: number) {
        this.time = time;
    },
    // 天亮的时间
    startTime: 6 * 60 * 60 * 1000,
    setStartTime(startTime: number = 6 * 60 * 60 * 1000) {
        this.startTime = startTime;
    },
    //天黑的时间
    endTime: 18 * 60 * 60 * 1000,
    setEndTime(endTime: number = 18 * 60 * 60 * 1000) {
        this.endTime = endTime;
    },


}, {}, {});

export default homeStore;